package com.pkk.penguin.frame.know.java.driven.propertiessources.config;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.EmbeddedValueResolverAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringValueResolver;

/**
 * 属性配置的
 *
 * @author peikunkun
 * @version V1.0
 * @date 2020-10-13 16:39
 **/
@Configuration
@PropertySource({"classpath:singleton.properties"})
public class PropertiesEmbeddedValueResolver implements EmbeddedValueResolverAware, InitializingBean {


  private StringValueResolver stringValueResolver;

  @Value("${singleton}")
  private String type1;

  private String type2;

  public PropertiesEmbeddedValueResolver(@Value("${singleton}") String type2) {
    this.type2 = type2;
  }

  /**
   * Set the StringValueResolver to use for resolving embedded definition values.
   *
   * @param resolver
   */
  @Override
  public void setEmbeddedValueResolver(StringValueResolver resolver) {
    this.stringValueResolver = resolver;
  }


  /**
   * Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and
   * ApplicationContextAware).
   * <p>This method allows the bean instance to perform initialization only
   * possible when all bean properties have been set and to throw an exception in the event of misconfiguration.
   *
   * @throws Exception in the event of misconfiguration (such as failure to set an essential property) or if
   * initialization fails.
   */
  @Override
  public void afterPropertiesSet() throws Exception {
    final String appName = stringValueResolver.resolveStringValue("${singleton}");
    System.out.println(this.getClass().getSimpleName() + " 【EmbeddedValueResolverAware】 获取参数配置 singleton:" + appName);
    System.out.println(this.getClass().getSimpleName() + " 【其他方式获取属性(@Value)】 获取参数配置 type1:" + type1);
    System.out.println(this.getClass().getSimpleName() + " 【其他方式获取属性(@Value)】 获取参数配置 type2:" + type2);
  }
}
